<template>
    <el-form ref="formRef" :model="formLabelAlign" :rules="rules" hide-required-asterisk label-position="top"
             label-width="100px">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model.trim="formLabelAlign.email"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="手机号码" prop="telephone">
                    <el-input v-model.trim="formLabelAlign.telephone"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="地址" prop="address">
                    <el-input v-model="formLabelAlign.address"/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="个人简介" prop="remark">
                    <el-input v-model="formLabelAlign.remark"/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-form-item>
            <el-button plain style="margin:auto" type="info" @click="reset(formRef)">刷新</el-button>
            <el-button plain style="margin:auto" type="primary" @click="updateAdmin">提交</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang='ts' setup>
import { ref } from 'vue'
import { Staff } from '@/ts/type'
import { StaffService } from '@/api'
import { useStaff } from '@/store'
import { Notification } from '@/plugins/element'
import { rules, staffInit } from '@/config/formConfig'
import { FormInstance } from 'element-plus'

const use_staff = useStaff()
const formRef = ref<FormInstance>()
const formLabelAlign = ref<Staff>({
  email: '',
  telephone: '',
  address: '',
  remark: ''
})

const updateAdmin = async () => {
  formLabelAlign.value.staffId = use_staff.info.staffId
  await StaffService.putStaff(formLabelAlign.value).then(res => {
    const { message, data } = res
    use_staff.$patch(state => {
      state.info = data
    })
    Notification.success(message)
  })
    .catch(() => {
      console.log('处理错误')
    })
  formLabelAlign.value = { ...staffInit }
}
const reset = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style lang='scss' scoped></style>